class Solution {
    public static String reverseStr(String s, int k) {
        int count = 0;
        int ret = 0;
        for (int i = 0; i < s.length(); i++) {
            count++;
            if (count % (2 * k) == 0) {
                s = reverse(s,ret,count - k - 1);
                ret += 2 * k;
            }
            if (count == s.length() && (count - ret < k)) {
                s = reverse(s,ret,s.length()-1);
            } else if (count == s.length() && (count - ret < (2 * k)) ) {
                s = reverse(s,ret,ret + k - 1);
            }
        }
        return s;
    }
    public static String reverse(String s,int left,int right) {
        char[] array = s.toCharArray();
        while (left <= right) {
            char tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }
        return String.valueOf(array);
    }

    public static void main(String[] args) {
        String s = "abcdefghijklmn";
        System.out.println(reverseStr(s,2));
    }
}